Assignment 8

Author

Thomas McHale

Published

April 12, 2023

## Load the API key
api_key <- Sys.getenv("ACS_API_KEY")

#load packages we may need
library(tidyverse)
library(lubridate)
library(gt)
library(paletteer)
library(ggridges)
library(plotly)
library(gtExtras)
library(sf)
library(tidycensus)
library(leaflet)
library(osmdata)
library(tigris)
library(ggplot2)
library(ggmap)
library(ggthemes)
library(viridis)

Task 3 from Assignment 6

#get health insurance data from ACS
mn_health <- get_acs(
  geography = "tract",
  variables = c("B27001_007" #uninsured
                , "B01003_001" #population
                ),
  state     = "MN",
  county    = "Hennepin",
  year      = 2020,
  geometry  = TRUE,
  cb        = FALSE,
)

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
mn_health_wide <- pivot_wider(
  data = mn_health,
  id_cols = c("GEOID", "geometry"),
  names_from = "variable",
  values_from = c("estimate", "moe")
)

#fraction percent uninsured
mn_health_wide$perc_uninsured <- mn_health_wide$estimate_B27001_007 / mn_health_wide$estimate_B01003_001

## create the map
health.gg <- ggplot() + 
  geom_sf(data = mn_health_wide, 
          aes(fill = perc_uninsured,
              text = paste("Percentage uninsured: ", 
                            scales::percent(perc_uninsured, accuracy = 0.1), 
                            "<br>Total population: ", estimate_B01003_001))) + 
  labs(title = "Hennepin County 2020 ACS Health Insurance") + 
  theme_void() + 
  scale_fill_viridis_c("Percentage uninsured", labels = scales::percent_format(accuracy = 1)) + 
  guides(fill = guide_colorbar(title.position = "top", title.hjust = 0.5)) +
  coord_sf(crs = st_crs("+proj=longlat +datum=WGS84")) # set the coordinate reference system to avoid warning
health.gg

ggplotly(health.gg, tooltip = "text") %>%
  layout(title = "Hennepin County 2020 ACS Health Insurance",
         xaxis = list(title = ""),
         yaxis = list(title = ""))